package com.hongyuan.sellfitness.ui.main.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivitySuccessBinding;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.main.view_mode.SuccessViewModel;

import me.tatarka.bindingcollectionadapter2.BR;

public class SuccessActivity extends CustomActivity<ActivitySuccessBinding, SuccessViewModel> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_success;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SuccessViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SuccessViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.comBack.setOnClickListener(view -> finish());

        RequestOptions options = new RequestOptions().skipMemoryCache(true);//配置
        Glide.with(this).asGif()
                .apply(options)//应用配置
                .load(R.drawable.success_gif_img)
                .listener(new RequestListener<GifDrawable>() {//添加监听，设置播放次数
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (resource instanceof GifDrawable) {
                            resource.setLoopCount(1);//只播放一次
                        }
                        return false;
                    }
                })
                .into(binding.showImg);
    }

    @Override
    public void initData() {
        super.initData();

        V3SuccessBeans beans = (V3SuccessBeans) getBundle().getSerializable("successBeans");
        viewModel.setEntity(beans);

    }
}
